// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
const tool = require('ToolUtil');
const GAMEID = tool.GAMEID;
const MJGAMETYPE = tool.MJGAMETYPE;
cc.Class({
    extends: cc.Component,

    properties: {
        pageNums: cc.Label,
        gameTitle: cc.Sprite,
        csmjSpriteFrames: cc.SpriteFrame,
        hzmjSpriteFrames: cc.SpriteFrame,
        zzmjSpriteFrames: cc.SpriteFrame,
        pdkSpriteFrames: cc.SpriteFrame,
        tablePreFab: cc.Prefab,
        tableArr: {
            default: [],
            type: [cc.Node]
        },
        _gameID: 0,
        _gameType: 0,
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        for (var i = 0; i < this.tableArr.length; i++) {
            var table = cc.instantiate(this.tablePreFab);
            table.x = this.tableArr[i].x;
            table.y = this.tableArr[i].y;
            this.node.addChild(table);
            this.tableArr[i].destroy();
            this.tableArr[i] = table;
            var btn = table.getComponent(cc.Button);
            var event = new cc.Component.EventHandler();
            event.target = this;
            event.component = 'TablesLayer';
            event.handler = 'onViewClick';
            event.customEventData = 'table' + (i + 1);
            btn.clickEvents.push(event);
            // btn.interactable = false;

        }
        // button.clickEvents.push(new cc.Component.EventHandler(...))
    },

    start: function () {

    },

    dismiss: function () {
        this.node.active = false;
    },

    quickCreateGame: function (gameid, rule) {
        cc.windowManager.showWindowByNameWithDate('loadingwin', "正在连接服务器,请稍等...");
        tool.requestWithGameID(gameid, "createTable", {
            config: rule
        }, function (data) {
            if (data.success) {
                if (data.url) {
                    this.dismiss();
                    if (cc.sys.isNative)
                        data.local = tool.getLocal();
                    // cc.sys.localStorage.setItem('gameconfig', JSON.stringify(this.mConfigMap));
                    tool.NetManager().connect(data.url, data.gameid, function (state) {
                        if (state == 'connect') {
                            tool.NetManager().sendPacket('CSCREATEROOM', {
                                config: data.config,
                                ticket: tool.getTicket()
                            });
                            cc.windowManager.dismissWindowByName('loadingwin');
                        }
                    }.bind(this));
                }
                return;
            } else {
                this.dismiss();
                cc.windowManager.dismissWindowByName('loadingwin');
                showMessageBox(data.msg);
            }
        }.bind(this), false);
    },

    onKyeBack: function () {
        this.node.active = false;
    },

    show: function (gametype) {
        this.node.active = true;
        switch (gametype) {
            case 'game_csmj':
                this._gameID = GAMEID.mj;
                this._gameType = MJGAMETYPE.CSMJ;
                this.gameTitle.spriteFrame = this.csmjSpriteFrames;
                break;
            case 'btn_hzmj':
                this._gameID = GAMEID.mj;
                this._gameType = MJGAMETYPE.HZMJ;
                this.gameTitle.spriteFrame = this.hzmjSpriteFrames;
                break;
            case 'game_zzmj':
                this._gameID = GAMEID.mj;
                this._gameType = MJGAMETYPE.ZZMJ;
                this.gameTitle.spriteFrame = this.zzmjSpriteFrames;
                break;
            case 'game_pdk':
                this._gameID = GAMEID.pdk;
                this.gameTitle.spriteFrame = this.pdkSpriteFrames;
                break;
        }
        this.getComponent('KeyControl').showDeafult();
    },

    onViewClick: function (view, data) {
        switch (data) {
            case 'createtable':
                this.quickCreateGame(this._gameID, {
                    "turn": 8,
                    "birdtype": 0,
                    "gametype": 0,
                    "hutype": 0,
                    "isPiao": false,
                    "isRandom": false,
                    "scoretype": 28,
                    "playnums": 3
                })
                break;
            case 'jointable':
                cc.windowManager.showWindowByNameWithDate('enterwin', 'enter');
                break;
            case 'up':
                break;
            case 'down':
                break;
            case 'back':
                this.node.active = false;
                break;
        }
    }

    // update (dt) {},
});